home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / ANSI / c-client / bezerk.h < prev    next >
Text File  |  1995-04-13  |  13KB  |  293 lines

  1. /*
  2.  * Program:    Berkeley mail routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    20 December 1989
  13.  * Last Edited:    12 April 1995
  14.  *
  15.  * Copyright 1995 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36.  
  37. /* Dedication:
  38.  *  This file is dedicated with affection to those Merry Marvels of Musical
  39.  * Madness . . .
  40.  *  ->  The Incomparable Leland Stanford Junior University Marching Band  <-
  41.  * who entertain, awaken, and outrage Stanford fans in the fact of repeated
  42.  * losing seasons and shattered Rose Bowl dreams [Cardinal just don't have
  43.  * HUSKY FEVER!!!].
  44.  *
  45.  */
  46.  
  47. /* Validate line
  48.  * Accepts: pointer to candidate string to validate as a From header
  49.  *        return pointer to end of date/time field
  50.  *        return pointer to offset from t of time (hours of ``mmm dd hh:mm'')
  51.  *        return pointer to offset from t of time zone (if non-zero)
  52.  * Returns: t,ti,zn set if valid From string, else ti is NIL
  53.  */
  54.  
  55. #define VALID(s,x,ti,zn) {                        \
  56.   ti = 0;                                \
  57.   if ((*s == 'F') && (s[1] == 'r') && (s[2] == 'o') && (s[3] == 'm') &&    \
  58.       (s[4] == ' ')) {                            \
  59.     for (x = s + 5; *x && *x != '\n'; x++);                \
  60.     if (*x) {                                \
  61.       if (x - s >= 41) {                        \
  62.     for (zn = -1; x[zn] != ' '; zn--);                \
  63.     if ((x[zn-1] == 'm') && (x[zn-2] == 'o') && (x[zn-3] == 'r') &&    \
  64.         (x[zn-4] == 'f') && (x[zn-5] == ' ') && (x[zn-6] == 'e') &&    \
  65.         (x[zn-7] == 't') && (x[zn-8] == 'o') && (x[zn-9] == 'm') &&    \
  66.         (x[zn-10] == 'e') && (x[zn-11] == 'r') && (x[zn-12] == ' '))\
  67.       x += zn - 12;                            \
  68.       }                                    \
  69.       if (x - s >= 27) {                        \
  70.     if (x[-5] == ' ') {                        \
  71.       if (x[-8] == ':') zn = 0,ti = -5;                \
  72.       else if (x[-9] == ' ') ti = zn = -9;                \
  73.       else if ((x[-11] == ' ') && ((x[-10]=='+') || (x[-10]=='-')))    \
  74.         ti = zn = -11;                        \
  75.     }                                \
  76.     else if (x[-4] == ' ') {                    \
  77.       if (x[-9] == ' ') zn = -4,ti = -9;                \
  78.     }                                \
  79.     else if (x[-6] == ' ') {                    \
  80.       if ((x[-11] == ' ') && ((x[-5] == '+') || (x[-5] == '-')))    \
  81.         zn = -6,ti = -11;                        \
  82.     }                                \
  83.     if (ti && !((x[ti - 3] == ':') &&                \
  84.             (x[ti -= ((x[ti - 6] == ':') ? 9 : 6)] == ' ') &&    \
  85.             (x[ti - 3] == ' ') && (x[ti - 7] == ' ') &&        \
  86.             (x[ti - 11] == ' '))) ti = 0;            \
  87.       }                                    \
  88.     }                                    \
  89.   }                                    \
  90. }
  91.  
  92. /* You are not expected to understand this macro, but read the next page if
  93.  * you are not faint of heart.
  94.  *
  95.  * Known formats to the VALID macro are:
  96.  *         From user Wed Dec  2 05:53 1992
  97.  * BSD        From user Wed Dec  2 05:53:22 1992
  98.  * SysV        From user Wed Dec  2 05:53 PST 1992
  99.  * rn        From user Wed Dec  2 05:53:22 PST 1992
  100.  *        From user Wed Dec  2 05:53 -0700 1992
  101.  *        From user Wed Dec  2 05:53:22 -0700 1992
  102.  *        From user Wed Dec  2 05:53 1992 PST
  103.  *        From user Wed Dec  2 05:53:22 1992 PST
  104.  *        From user Wed Dec  2 05:53 1992 -0700
  105.  * Solaris    From user Wed Dec  2 05:53:22 1992 -0700
  106.  *
  107.  * Plus all of the above with `` remote from xxx'' after it. Thank you very
  108.  * much, smail and Solaris, for making my life considerably more complicated.
  109.  */
  110.  
  111. /*
  112.  * What?  You want to understand the VALID macro anyway?  Alright, since you
  113.  * insist.  Actually, it isn't really all that difficult, provided that you
  114.  * take it step by step.
  115.  *
  116.  * Line 1    Initializes the return ti value to failure (0);
  117.  * Lines 2-3    Validates that the 1st-5th characters are ``From ''.
  118.  * Lines 4-5    Validates that there is an end of line and points x at it.
  119.  * Lines 6-13    First checks to see if the line is at least 41 characters long.
  120.  *        If so, it scans backwards to find the rightmost space.  From
  121.  *        that point, it scans backwards to see if the string matches
  122.  *        `` remote from''.  If so, it sets x to point to the space at
  123.  *        the start of the string.
  124.  * Line 14    Makes sure that there are at least 27 characters in the line.
  125.  * Lines 15-20    Checks if the date/time ends with the year (there is a space
  126.  *        five characters back).  If there is a colon three characters
  127.  *        further back, there is no timezone field, so zn is set to 0
  128.  *        and ti is set in front of the year.  Otherwise, there must
  129.  *        either to be a space four characters back for a three-letter
  130.  *        timezone, or a space six characters back followed by a + or -
  131.  *        for a numeric timezone; in either case, zn and ti become the
  132.  *        offset of the space immediately before it.
  133.  * Lines 21-23    Are the failure case for line 14.  If there is a space four
  134.  *        characters back, it is a three-letter timezone; there must be a
  135.  *        space for the year nine characters back.  zn is the zone
  136.  *        offset; ti is the offset of the space.
  137.  * Lines 24-27    Are the failure case for line 20.  If there is a space six
  138.  *        characters back, it is a numeric timezone; there must be a
  139.  *        space eleven characters back and a + or - five characters back.
  140.  *        zn is the zone offset; ti is the offset of the space.
  141.  * Line 28-31    If ti is valid, make sure that the string before ti is of the
  142.  *        form www mmm dd hh:mm or www mmm dd hh:mm:ss, otherwise
  143.  *        invalidate ti.  There must be a colon three characters back
  144.  *        and a space six or nine    characters back (depending upon
  145.  *        whether or not the character six characters back is a colon).
  146.  *        There must be a space three characters further back (in front
  147.  *        of the day), one seven characters back (in front of the month),
  148.  *        and one eleven characters back (in front of the day of week).
  149.  *        ti is set to be the offset of the space before the time.
  150.  *
  151.  * Why a macro?  It gets invoked a *lot* in a tight loop.  On some of the
  152.  * newer pipelined machines it is faster being open-coded than it would be if
  153.  * subroutines are called.
  154.  *
  155.  * Why does it scan backwards from the end of the line, instead of doing the
  156.  * much easier forward scan?  There is no deterministic way to parse the
  157.  * ``user'' field, because it may contain unquoted spaces!  Yes, I tested it to
  158.  * see if unquoted spaces were possible.  They are, and I've encountered enough
  159.  * evil mail to be totally unwilling to trust that ``it will never happen''.
  160.  */
  161.  
  162. /* Build parameters */
  163.  
  164. #define KODRETRY 15        /* kiss-of-death retry in seconds */
  165. #define LOCKTIMEOUT 5        /* lock timeout in minutes */
  166. #define CHUNK 32768        /* read-in chunk size */
  167.  
  168.  
  169. /* Command bits from bezerk_getflags() */
  170.  
  171. #define fSEEN 1
  172. #define fDELETED 2
  173. #define fFLAGGED 4
  174. #define fANSWERED 8
  175.  
  176.  
  177. /* Status string */
  178.  
  179. #define STATUS "Status: RO\nX-Status: DFA\n\n"
  180.  
  181. /* BEZERK per-message cache information */
  182.  
  183. typedef struct file_cache {
  184.   char *header;            /* pointer to RFC 822 header */
  185.   unsigned long headersize;    /* size of RFC 822 header */
  186.   char *body;            /* pointer to message body */
  187.   unsigned long bodysize;    /* size of message body */
  188.   char status[sizeof (STATUS)];    /* status flags */
  189.   char internal[1];        /* start of internal header and data */
  190. } FILECACHE;
  191.  
  192.  
  193. /* BEZERK I/O stream local data */
  194.     
  195. typedef struct bezerk_local {
  196.   unsigned int dirty : 1;    /* disk copy needs updating */
  197.   int ld;            /* lock file descriptor */
  198.   char *name;            /* local file name for recycle case */
  199.   char *lname;            /* lock file name */
  200.   off_t filesize;        /* file size parsed */
  201.   time_t filetime;        /* last file time */
  202.   unsigned long cachesize;    /* size of local cache */
  203.   FILECACHE **msgs;        /* pointers to message-specific information */
  204.   char *buf;            /* temporary buffer */
  205.   unsigned long buflen;        /* current size of temporary buffer */
  206. } BEZERKLOCAL;
  207.  
  208.  
  209. /* Convenient access to local data */
  210.  
  211. #define LOCAL ((BEZERKLOCAL *) stream->local)
  212.  
  213. /* Function prototypes */
  214.  
  215. DRIVER *bezerk_valid (char *name);
  216. int bezerk_isvalid (char *name,char *tmp);
  217. long bezerk_isvalid_fd (int fd,char *tmp);
  218. void *bezerk_parameters (long function,void *value);
  219. void bezerk_find (MAILSTREAM *stream,char *pat);
  220. void bezerk_find_bboards (MAILSTREAM *stream,char *pat);
  221. void bezerk_find_all (MAILSTREAM *stream,char *pat);
  222. void bezerk_find_all_bboards (MAILSTREAM *stream,char *pat);
  223. long bezerk_subscribe (MAILSTREAM *stream,char *mailbox);
  224. long bezerk_unsubscribe (MAILSTREAM *stream,char *mailbox);
  225. long bezerk_subscribe_bboard (MAILSTREAM *stream,char *mailbox);
  226. long bezerk_unsubscribe_bboard (MAILSTREAM *stream,char *mailbox);
  227. long bezerk_create (MAILSTREAM *stream,char *mailbox);
  228. long bezerk_delete (MAILSTREAM *stream,char *mailbox);
  229. long bezerk_rename (MAILSTREAM *stream,char *old,char *new);
  230. MAILSTREAM *bezerk_open (MAILSTREAM *stream);
  231. void bezerk_close (MAILSTREAM *stream);
  232. void bezerk_fetchfast (MAILSTREAM *stream,char *sequence);
  233. void bezerk_fetchflags (MAILSTREAM *stream,char *sequence);
  234. ENVELOPE *bezerk_fetchstructure (MAILSTREAM *stream,long msgno,BODY **body);
  235. char *bezerk_snarf (MAILSTREAM *stream,long msgno,long *size);
  236. char *bezerk_fetchheader (MAILSTREAM *stream,long msgno);
  237. char *bezerk_fetchtext (MAILSTREAM *stream,long msgno);
  238. char *bezerk_fetchbody(MAILSTREAM *stream,long m,char *sec,unsigned long *len);
  239. void bezerk_setflag (MAILSTREAM *stream,char *sequence,char *flag);
  240. void bezerk_clearflag (MAILSTREAM *stream,char *sequence,char *flag);
  241. void bezerk_search (MAILSTREAM *stream,char *criteria);
  242. long bezerk_ping (MAILSTREAM *stream);
  243. void bezerk_check (MAILSTREAM *stream);
  244. void bezerk_expunge (MAILSTREAM *stream);
  245. long bezerk_copy (MAILSTREAM *stream,char *sequence,char *mailbox);
  246. long bezerk_move (MAILSTREAM *stream,char *sequence,char *mailbox);
  247. long bezerk_append (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
  248.             STRING *message);
  249. long bezerk_append_putc (int fd,char *s,int *i,char c);
  250. void bezerk_gc (MAILSTREAM *stream,long gcflags);
  251.  
  252. void bezerk_abort (MAILSTREAM *stream);
  253. char *bezerk_file (char *dst,char *name);
  254. int bezerk_lock (char *file,int flags,int mode,char *lock,int op);
  255. void bezerk_unlock (int fd,MAILSTREAM *stream,char *lock);
  256. int bezerk_parse (MAILSTREAM *stream,char *lock,int op);
  257. char *bezerk_eom (char *som,char *sod,long i);
  258. int bezerk_extend (MAILSTREAM *stream,int fd,char *error);
  259. void bezerk_save (MAILSTREAM *stream,int fd);
  260. int bezerk_copy_messages (MAILSTREAM *stream,char *mailbox);
  261. int bezerk_write_message (int fd,FILECACHE *m);
  262. void bezerk_update_status (char *status,MESSAGECACHE *elt);
  263. short bezerk_getflags (MAILSTREAM *stream,char *flag);
  264. char bezerk_search_all (MAILSTREAM *stream,long msgno,char *d,long n);
  265. char bezerk_search_answered (MAILSTREAM *stream,long msgno,char *d,long n);
  266. char bezerk_search_deleted (MAILSTREAM *stream,long msgno,char *d,long n);
  267. char bezerk_search_flagged (MAILSTREAM *stream,long msgno,char *d,long n);
  268. char bezerk_search_keyword (MAILSTREAM *stream,long msgno,char *d,long n);
  269. char bezerk_search_new (MAILSTREAM *stream,long msgno,char *d,long n);
  270. char bezerk_search_old (MAILSTREAM *stream,long msgno,char *d,long n);
  271. char bezerk_search_recent (MAILSTREAM *stream,long msgno,char *d,long n);
  272. char bezerk_search_seen (MAILSTREAM *stream,long msgno,char *d,long n);
  273. char bezerk_search_unanswered (MAILSTREAM *stream,long msgno,char *d,long n);
  274. char bezerk_search_undeleted (MAILSTREAM *stream,long msgno,char *d,long n);
  275. char bezerk_search_unflagged (MAILSTREAM *stream,long msgno,char *d,long n);
  276. char bezerk_search_unkeyword (MAILSTREAM *stream,long msgno,char *d,long n);
  277. char bezerk_search_unseen (MAILSTREAM *stream,long msgno,char *d,long n);
  278. char bezerk_search_before (MAILSTREAM *stream,long msgno,char *d,long n);
  279. char bezerk_search_on (MAILSTREAM *stream,long msgno,char *d,long n);
  280. char bezerk_search_since (MAILSTREAM *stream,long msgno,char *d,long n);
  281. char bezerk_search_body (MAILSTREAM *stream,long msgno,char *d,long n);
  282. char bezerk_search_subject (MAILSTREAM *stream,long msgno,char *d,long n);
  283. char bezerk_search_text (MAILSTREAM *stream,long msgno,char *d,long n);
  284. char bezerk_search_bcc (MAILSTREAM *stream,long msgno,char *d,long n);
  285. char bezerk_search_cc (MAILSTREAM *stream,long msgno,char *d,long n);
  286. char bezerk_search_from (MAILSTREAM *stream,long msgno,char *d,long n);
  287. char bezerk_search_to (MAILSTREAM *stream,long msgno,char *d,long n);
  288.  
  289. typedef char (*search_t) (MAILSTREAM *stream,long msgno,char *d,long n);
  290. search_t bezerk_search_date (search_t f,long *n);
  291. search_t bezerk_search_flag (search_t f,char **d);
  292. search_t bezerk_search_string (search_t f,char **d,long *n);
  293.